package com.lj.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lj.mp.standard.StandardServiceImpl;
import com.lj.mp.utils.PageQueryUtils;
import com.lj.sys.entity.SysDataScope;
import com.lj.sys.mapper.SysDataScopeMapper;
import com.lj.sys.param.SysDataScopePageParam;
import com.lj.sys.param.SysDataScopeSaveParam;
import com.lj.sys.param.SysDataScopeUpdateParam;
import com.lj.sys.result.SysDataScopeInfoResult;
import com.lj.sys.result.SysDataScopePageResult;
import com.lj.sys.service.SysDataScopeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据范围表 服务实现类
 * </p>
 *
 * @author lj
 * @since 2025-08-06 18:24:15
 */
@Service
public class SysDataScopeServiceImpl extends StandardServiceImpl<SysDataScopeMapper, SysDataScope> implements SysDataScopeService {


    @Override
    public IPage<SysDataScopePageResult> page(SysDataScopePageParam param) {
        return this.page(PageQueryUtils.getPage(param), getQueryWrapper(param)).convert(SysDataScopePageResult::of);
    }

    private LambdaQueryWrapper<SysDataScope> getQueryWrapper(SysDataScopePageParam param) {
        return lambdaQueryWrapper()
                .eq(SysDataScope::getTargetId, param.getTargetId())
                .orderByDesc(CollUtil.isEmpty(param.getSortList()), SysDataScope::getId);
    }

    @Override
    public SysDataScopeInfoResult info(Long id) {
        return SysDataScopeInfoResult.of(this.getById(id));
    }

    @Override
    public void save(SysDataScopeSaveParam param) {
        SysDataScope entity = param.toEntity();
        this.save(entity);
    }

    @Override
    public void update(SysDataScopeUpdateParam param) {
        SysDataScope entity = param.toEntity();
        this.updateById(entity);
    }

    @Override
    public void delete(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        this.removeByIds(ids);
    }
}
